package com.book.controller.system;

import com.book.auth.service.LoginService;
import com.book.core.domain.SysUser;
import com.book.core.dto.LoginDTO;
import com.book.core.mode.R;
import com.book.core.query.SysUserQuery;
import com.book.core.vo.PageVO;
import com.book.core.vo.SysUserViewVO;
import com.book.service.SysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Set;

@RestController
@RequiredArgsConstructor
@RequestMapping("/system/user")
public class SysUserController {
    private final SysUserService sysUserService;
    private final RedisTemplate<String, String> redisTemplate;

    @GetMapping("/page")
    public R<PageVO<SysUserViewVO>> getUserPage(SysUserQuery sysUserQuery) {
        PageVO<SysUserViewVO> userPage = sysUserService.getUserPage(sysUserQuery);
        return R.ok(userPage);
    }

    @PostMapping
    public R save(@RequestBody @Validated SysUser sysUser) {
        sysUserService.createSysUser(sysUser);
        return R.ok("新增成功", null);
    }

    @PutMapping
    public R update(@RequestBody @Validated SysUser sysUser) {
        sysUserService.updateById(sysUser);
        return R.ok("删除成功", null);
    }

    @DeleteMapping("/{id}")
    public R delete(@PathVariable Integer id) {
        sysUserService.removeById(id);
        return R.ok();
    }

    @GetMapping("/online/count")
    public R getOnlineCount() {
        Set<String> keys = redisTemplate.keys("loginUser:*");
        int count = keys != null ? keys.size() : 0;
        return R.ok(count);
    }
}
